package com.laf.manager.service.impl;

import com.laf.manager.controller.dto.PointsDeductionDto;
import com.laf.manager.dto.PointsDeduction;
import com.laf.manager.querycondition.DeductionQueryCondition;
import com.laf.manager.repository.PointsDeductionRepository;
import com.laf.manager.schedule.ScheduleConfig;
import com.laf.manager.service.PointsDeductionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author ygl
 * @create 2019-01-02
 * @DESCRIPTION
 **/
@Slf4j
@Service
public class PointsDeductionServiceImpl implements PointsDeductionService {

    @Autowired
    private PointsDeductionRepository repository;

    @Override
    public List<PointsDeduction> findByStatus(int status) {
        return repository.findByStatus(status);
    }

    @Override
    public void updateStatus(PointsDeduction model) {
        repository.updateStatus(model);
    }

    @Override
    public List<PointsDeduction> findByOffsetNumber(String offsetNumber) {
        return repository.findByOffsetNumber(offsetNumber);
    }

    @Override
    public List<PointsDeductionDto> findAll() {
        return repository.findAll();
    }

    @Override
    public List<PointsDeductionDto> findByMemberName(String memberName) {
        return repository.findByMemberName(memberName);
    }

    @Override
    public List<PointsDeductionDto> getList(DeductionQueryCondition condition) {
        return repository.getList(condition);
    }

    @Override
    public int getCountList(DeductionQueryCondition condition) {
        return repository.getCountList(condition);
    }

    @Override
    public boolean startTask(ScheduleConfig scheduleConfig,int minute) {
        String cron = "0 0/"+minute+" * * * ? ";
        if(!StringUtils.isEmpty(cron)) {
            scheduleConfig.setCron(cron);
            log.info("开启定时任务成功"+cron);
            return true;
        } else {
            log.error("开启定时任务失败");
            return false;
        }
    }

    @Override
    public boolean closeTask(ScheduleConfig scheduleConfig) {
        try {
            scheduleConfig.stopCron();
            log.info("关闭定时任务成功");
            return true;
        }catch (Exception e){
            log.error("关闭定时任务失败");
        }
        return false;
    }
}
